//
//  UIViewFrameExtension.swift
//  library
//
//  Created by 温州芮智达 on 2022/11/24.
//

import UIKit

extension UIView{
    
    
// MARK: - Size和四角坐标
    
    
    /**
     对self.frame.size的设置和获取
     */
    public var size: CGSize {
        set{
            var new = self.frame
            new.size = newValue
            self.frame = new
        }
        get{
            self.frame.size
        }
    }
    
    
    /**
     对 self.frame.origin的设置和获取 左上角
     */
    public var origin: CGPoint {
        set{
            var new = self.frame
            new.origin = newValue
            self.frame = new
        }
        get{
            return self.frame.origin
        }
    }
    
    
    /**
     获取右上角的坐标
     */
    public private(set) var topRight: CGPoint {
        set{}
        get{
            let x = self.frame.origin.x + self.frame.size.width
            let y = self.frame.origin.y
            return CGPoint(x: x, y: y)
        }
    }
    
    
    /**
     获取左下角的坐标
     */
    public private(set) var bootomLeft: CGPoint  {
        set{}
        get{
            let x = self.frame.origin.x
            let y = self.frame.origin.y + self.frame.size.height
            return CGPoint(x: x, y: y)
        }
    }
    
    /**
     获取右下角的坐标
     */
    public private(set) var bottomRight: CGPoint  {
        set{}
        get{
            let x = self.frame.origin.x + self.frame.size.width
            let y = self.frame.origin.y + self.frame.size.height
            return CGPoint(x: x, y: y)
        }
    }
    
    
    
    
// MARK: - 宽高和上下左右中
    /**
     宽度
     */
    public var width: CGFloat {
        set{
            var new = self.frame
            new.size.width = newValue
            self.frame = new
        }
        get{
            return self.frame.size.width
        }
    }
    
    
    /**
     高度
     */
    public var height: CGFloat {
        set{
            var new = self.frame
            new.size.height = newValue
            self.frame = new
        }
        get{
            return self.frame.size.height
        }
    }
    
    
    /**
     上边
     */
    public var top: CGFloat {
        set{
            var new = self.frame
            new.origin.y = newValue
            self.frame = new
        }
        get{
            return self.frame.origin.y
        }
    }
    
    /**
     底边
     */
    public var bottom: CGFloat {
        set{
            var new = self.frame
            new.origin.y = newValue - self.frame.size.height
            self.frame = new
        }
        get{
            self.frame.origin.y + self.frame.size.height
        }
    }
    
    /**
     左边
     */
    public var left: CGFloat {
        set{
            var new = self.frame
            new.origin.x = newValue
            self.frame = new
        }
        get{
            return self.frame.origin.x
        }
    }
    
    /**
     右
     */
    public var right: CGFloat {
        set{
            var new = self.frame
            new.origin.x = newValue - self.frame.size.width
            self.frame = new
        }
        get{
            return self.frame.origin.x + self.frame.size.width
        }
    }
    
    
    /**
     中心 x 坐标
     */
    public var centerX: CGFloat {
        set{
            var new = self.center
            new.x = newValue
            self.center = new
        }
        get{
            return self.center.x
        }
    }
    
    
    /**
     中心 y 坐标
     */
    public var centerY:CGFloat {
        set{
            var new = self.center
            new.y = newValue
            self.center = new
        }
        get{
            return self.center.y
        }
    }
    
    /**
     内部中心坐标
     */
    public private(set) var boundCenter: CGPoint {
        set{}
        get{
            return .init(x: self.bounds.midX, y: self.bounds.midY)
        }
    }
    
// MARK: - 偏移量移动和size缩放
    /**
     偏移量移动
     x,y + 偏移量
     */
    public func moveBy(delta: CGPoint) {
        var new = self.center
        new.x += delta.x
        new.y += delta.y
        self.center = new
    }
    
    
    /**
     size缩放倍数
     操作size的缩放
     */
    public func scaleBy(scale: CGFloat) {
        var new = self.frame
        new.size.width *= scale
        new.size.height *= scale
        self.frame = new
    }
    
    
}


